﻿
using System;
using System.Collections.Generic;
using System.Data;
using QLKS.Entity;

namespace QLKS.DAL
{
    public class ChiTietHoaDonDAO
    {
        private SqlDataProvider dataProvider;

        public ChiTietHoaDonDAO()
        {
            dataProvider = new SqlDataProvider();
        }

        public List<ChiTietHoaDon> GetByAll(ChiTietHoaDon obj)
        {
            List<ChiTietHoaDon> lstData = new List<ChiTietHoaDon>(); ;
            try
            {
                DataTable dt = dataProvider.ExecuteDataTable("sp_ChiTietHoaDon_GetByAll"
                    , new string[] { "MaHoaDon", "MaPhong", "SoNgayThue", "DonGia", "ThanhTien" }
                    , new object[] { obj.MaHoaDon, obj.MaPhong, obj.SoNgayThue, obj.DonGia, obj.ThanhTien }
                );
                ChiTietHoaDon entity;
                foreach(DataRow myrow in dt.Rows)
                {
                    entity = new ChiTietHoaDon();
                    entity.MaHoaDon = Convert.ToInt32(myrow["MaHoaDon"]);
                    entity.MaPhong = Convert.ToInt32(myrow["MaPhong"]);
                    entity.SoNgayThue = Convert.ToInt32(myrow["SoNgayThue"]);
                    entity.DonGia = Convert.ToDouble(myrow["DonGia"]);
                    entity.ThanhTien = Convert.ToDouble(myrow["ThanhTien"]);
                    lstData.Add(entity);
                }
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lstData;
        }

        public bool Insert(ChiTietHoaDon obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_ChiTietHoaDon_Insert"
                    , new string[] { "MaHoaDon", "MaPhong", "SoNgayThue", "DonGia", "ThanhTien" }
                    , new object[] { obj.MaHoaDon, obj.MaPhong, obj.SoNgayThue, obj.DonGia, obj.ThanhTien }
                );
            return i == 1 ? true : false;
        }

        public bool Update(ChiTietHoaDon obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_ChiTietHoaDon_Update"
                    , new string[] { "MaHoaDon", "MaPhong", "SoNgayThue", "DonGia", "ThanhTien" }
                    , new object[] { obj.MaHoaDon, obj.MaPhong, obj.SoNgayThue, obj.DonGia, obj.ThanhTien }
                );
            return i == 1 ? true : false;
        }

        public bool Delete(ChiTietHoaDon obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_ChiTietHoaDon_Delete"
                    , new string[] { "MaHoaDon", "MaPhong", "SoNgayThue", "DonGia", "ThanhTien" }
                    , new object[] { obj.MaHoaDon, obj.MaPhong, obj.SoNgayThue, obj.DonGia, obj.ThanhTien }
                );
            return i == 1 ? true : false;
        }
    }
}
